package com.demo.orders;

import com.demo.book.BookService;
import com.demo.common.constant.WebConstants;
import com.demo.common.model.Borrow;
import com.demo.common.model.Orders;
import com.demo.common.util.StringUtil;
import com.demo.student.StudentService;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;

import java.util.Date;

@Before(OrdersInterceptor.class)
public class OrdersController extends Controller {

    @Inject
    OrdersService orderService;

    @Inject
    BookService bookService;

    @Inject
    StudentService studentService;

    public void index() {
        Kv cond=Kv.create();
        if(StrKit.notBlank(getPara("studentid"))){
            cond.set("studentid",getPara("studentid"));
        }
        if(StrKit.notBlank(getPara("isorder"))){
            cond.set("isorder",getPara("isorder"));
        }
        setAttr("orderPage", orderService.paginate(getParaToInt(0, 1), 10,Kv.by("cond",cond)));
        render("orders.html");
    }

    public void add() {
        setAttr("students",studentService.findAll());
        setAttr("books", bookService.findAll());
    }

    /**
     * save 与 update 的业务逻辑在实际应用中也应该放在 serivce 之中，
     * 并要对数据进正确性进行验证，在此仅为了偷懒
     */
    @Before(OrdersValidator.class)
    public void save() {
        Orders order=getBean(Orders.class);
        order.setBookcopyid(bookService.findByBookId(order.getBookid()).getBookcopyid());
        order.save();
        redirect("/orders");
    }

    public void edit() {
        setAttr("students",studentService.findAll());
        setAttr("books", bookService.findAll());
        setAttr("orders", orderService.findById(getParaToInt()));
    }

    /**
     * save 与 update 的业务逻辑在实际应用中也应该放在 serivce 之中，
     * 并要对数据进正确性进行验证，在此仅为了偷懒
     */
    @Before(OrdersValidator.class)
    public void update() {
        Orders order=getBean(Orders.class);
        order.setBookcopyid(bookService.findByBookId(order.getBookid()).getBookcopyid());
        order.update();
        redirect("/orders");
    }

    public void delete() {
        orderService.deleteById(getParaToInt());
        redirect("/orders");
    }

    //取消预约
    public void isorder(){
        Orders orders=new Orders();
        orders.setId(getParaToInt());
        orders.setIsorder(WebConstants.NO_ORDER);
        orders.update();
        redirect("/orders");
    }

    //恢复预约
    public void isnoorder(){
        Orders orders=new Orders();
        orders.setId(getParaToInt());
        orders.setIsorder(WebConstants.ORDER);
        orders.update();
        redirect("/orders");
    }


}
